<%--
  Created by IntelliJ IDEA.
  User: 沐白
  Date: 2020/8/20
  Time: 15:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>新闻管理</title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/laydate/need/laydate.css"/>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/toastr/toastr.min.css"/>
    <script src="${pageContext.request.contextPath}/static/js/jquery-2.1.0.js" type="text/javascript"
            charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/static/js/bootstrap.min.js" type="text/javascript"
            charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/static/laydate/laydate.js"></script>
    <script src="${pageContext.request.contextPath}/static/toastr/toastr.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/Myjs.js"></script>
    <style type="text/css">
        .table-bordered > tbody > tr > td {
            vertical-align: middle;
        }
    </style>
</head>

<body>
<!--全局盒子结束 -->
<div class="form-group" style="margin-top: 10px">
    <!--全局盒子 -->
    <div class="form-group">
        <!--顶部盒子 -->
        <table>
            <tr>
                <td>
                    <label>新闻/公告标题</label>
                </td>
                <td>
                    <input type="text" class="form-control" id="newstitle" placeholder="新闻/公告标题"/>
                </td>
                <td>
                    <label>发布人</label>
                </td>
                <td>
                    <input type="text" class="form-control" id="newsname" placeholder="发布人"/>
                </td>
                <td>
                    <label>开始时间</label>
                </td>
                <td>
                    <input type="text" class="form-control" id="ksshijian" placeholder="开始时间"/>
                </td>
                <td>
                    <label>结束时间</label>
                </td>
                <td>
                    <input type="text" class="form-control" id="jsshijian" placeholder="结束时间"/>
                </td>
                <td>
                    <input type="button" class="btn btn-primary" id="shoubtn" value="搜索"/>
                </td>
                <td>
                    <input type="button" data-toggle="modal" data-target="#xinzeng" class="btn btn-primary"
                           value="新增新闻/公告"/>
                </td>
            </tr>
        </table>
    </div>
    <!--顶部盒子结束 -->
    <div class="form-group">
        <!--中部盒子 -->
        <table class="table table-striped table-bordered text-center table-hover">
            <tr class="info">
                <td>排序</td>
                <td class="col-sm-5">标题</td>
                <td>公告状态</td>
                <td>发布人</td>
                <td>创建日期</td>
                <td>结束日期</td>
                <td>操作</td>
            </tr>
            <tbody id="tb"></tbody>
        </table>
    </div>
    <!--中部盒子结束 -->
    <div class="form-inline">
        <!--下部盒子 -->
        <%--下边的文字下拉菜单还有按钮 --%>
        <div class="form-inline">
            <label>显示第 <span id="rowstart"></span> 到第 <span id="rowend"></span> 条记录,总共<span id="rowCount"></span>条记录
                每页显示
                <select class="form-control" id="pageSize">
                    <option value="5">5</option>
                    <option value="10">10</option>
                    <option value="20">20</option>
                    <option value="50">50</option>
                    <option value="100">100</option>
                </select>
                条记录</label>
            <label class="btn-group pull-right">
                <a href="javascript:search(pageBean.pageNo-1)" class="btn btn-default"><</a>
                <div id="btns" class="btn-group"></div>
                <a href="javascript:search(pageBean.pageNo+1)" class="btn btn-default">></a>
            </label>
        </div>
    </div>
    <!--下部盒子结束 -->
</div>
<!-- 修改模态框 -->
<div class="modal" id="xiugai">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <!--模态框头部 -->
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">修改新闻信息</h4>
            </div>
            <div class="modal-body">
                <!--模态框内容 -->
                <form id="xgform">
                    <div class="row">
                        <div class="form-group col-sm-12">
                            <div class="input-group  col-sm-12">
                                <input type="hidden" name="id" value="">
                                <div class="input-group-addon">
                                    新闻公告标题
                                </div>
                                <input type="text" class="form-control" name="title" placeholder="新闻公告内容"/>
                                <span class="glyphicon form-control-feedback"></span>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-sm-6">
                            <div class="input-group col-sm-11">
                                <div class="input-group-addon">发布人</div>
                                <input type="text" class="form-control" name="staffName"
                                       disabled="disabled"/>
                            </div>
                        </div>
                        <div class="form-group col-sm-6">
                            <div class="input-group col-sm-12">
                                <div class="input-group-addon">状态</div>
                                <select class="form-control" name="status">
                                    <option value="1">启动</option>
                                    <option value="0">禁用</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-sm-6">
                            <div class="input-group col-sm-11">
                                <div class="input-group-addon">创建时间</div>
                                <input type="text" id="cjshijian1" class="form-control" name="createdTime"
                                       placeholder="创建时间" disabled="disabled"/>
                            </div>
                        </div>
                        <div class="form-group col-sm-6">
                            <div class="input-group col-sm-12">
                                <div class="input-group-addon">结束时间</div>
                                <input type="text" id="jieshusj1" class="form-control" name="endTime"
                                       placeholder="结束时间"/>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-sm-12">
                            <div class="input-group col-sm-12">
                                <div class="input-group-addon">
                                    新闻公告内容
                                </div>
                                <textarea class="form-control" rows="3" name="content" placeholder="新闻公告内容"></textarea>
                                <span class="glyphicon form-control-feedback"></span>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <!--模态框下边的按钮 -->
                <input type="button" class="btn btn-default" data-dismiss="modal" value="关闭"/>
                <input type="button" class="btn btn-info" onclick="xgqueren()" value="确定"/>
            </div>
        </div>
    </div>
</div>
<!-- 新增模态框 -->
<div class="modal" id="xinzeng">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <!--模态框头部 -->
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">新增新闻信息</h4>
            </div>
            <div class="modal-body">
                <!--模态框内容 -->
                <form id="xzform">
                    <div class="row">
                        <div class="form-group col-sm-12">
                            <div class="input-group  col-sm-12">
                                <div class="input-group-addon">新闻公告标题</div>
                                <input type="text" class="form-control" name="title" placeholder="新闻公告标题"/>
                                <span class="glyphicon form-control-feedback"></span>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-sm-6">
                            <div class="input-group col-sm-11">
                                <div class="input-group-addon">发布人</div>
                                <input type="text" class="form-control" name="staffName" id="xzstaff" disabled="disabled"/>
                            </div>
                        </div>
                        <div class="form-group col-sm-6">
                            <div class="input-group col-sm-12">
                                <div class="input-group-addon">状态</div>
                                <select class="form-control" name="status">
                                    <option value="1">启动</option>
                                    <option value="0">禁用</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-sm-6">
                            <div class="input-group col-sm-11">
                                <div class="input-group-addon">创建时间</div>
                                <input type="text" id="cjshijian" name="createdTime" disabled class="form-control"
                                       placeholder="创建时间"/>
                            </div>
                        </div>
                        <div class="form-group col-sm-6">
                            <div class="input-group col-sm-12">
                                <div class="input-group-addon">结束时间</div>
                                <input type="text" id="jieshusj" name="endTime" class="form-control"
                                       placeholder="结束时间"/>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-sm-12">
                            <div class="input-group col-sm-12">
                                <div class="input-group-addon">
                                    新闻公告内容
                                </div>
                                <textarea class="form-control" rows="3" name="content" placeholder="新闻公告内容"></textarea>
                                <span class="glyphicon form-control-feedback"></span>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <!--模态框下边的按钮 -->
                <input type="button" class="btn btn-default" data-dismiss="modal" value="关闭"/>
                <input type="button" class="btn btn-info" id="xzqueren" value="确定"/>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
    let pageBean = new Object();
    pageBean.pageNo = 1;
    pageBean.pageSize = 5;
    pageBean.factor = [];
    $(function () {
        //开始的时候查询
        search(1);
        //点击搜索按钮查询
        $('#shoubtn').click(function () {
            search(1);
        });
        $('#xiugai').on('hide.bs.modal',function () {
            $(this).clea();
        })
        $('#xinzeng').on('show.bs.modal',function () {
            $('[name="createdTime"]').val(new Date().format("yyyy-MM-dd"));
        })
        //新增修改确认按钮事件
        $('#xzqueren').click(function () {
            if ($('#xzform [name="title"]').val() != "" && $('#xzform [name="content"]').val() != "") {
                if ($('#xzform [name="createdTime"]').val() != "", $('#xzform [name="endTime"]').val() != "") {
                    if (validTime($('#xzform [name="createdTime"]').val(), $('#xzform [name="endTime"]').val())) {
                        $.post(
                            "${pageContext.request.contextPath}/newsController/add",
                            $('#xzform').parseForm(),
                            function (date) {
                                $('#xinzeng').modal('hide');
                                show();
                                if (date > 0) {
                                    toastr.success("添加成功");
                                } else {
                                    toastr.error("添加失败");
                                }
                            },
                            "json"
                        );
                    } else {
                        toastr.info("开始时间不能超过结束时间");
                    }
                } else {
                    toastr.info("请输入开始时间和结束时间");
                }
            } else {
                toastr.info("请输入标题和内容");
            }
        });
        //行数进行改变事件
        $('#pageSize').on('change', function () {
            pageBean.pageSize = parseInt($('#pageSize').val());
            search(1);
        });
        //新闻新增输入框监听 事件
        $('[name="title"]').each(function () {
            $(this).on('input porpertychange', function () {
                if ($(this).val() == "") {
                    this.parentNode.className = 'input-group col-sm-12 has-error';
                    this.parentNode.getElementsByClassName('glyphicon')[0].className = 'glyphicon form-control-feedback glyphicon-remove';
                } else {
                    this.parentNode.className = 'input-group col-sm-12 has-success';
                    this.parentNode.getElementsByClassName('glyphicon')[0].className = 'glyphicon form-control-feedback glyphicon-ok';
                }
            })
        })
        //新闻修改事件
        /*$('#xiugai .form-control').each(function() {
            $(this).on('input porpertychange', function() {
                if($(this).val() == "") {
                    this.parentNode.className = 'input-group col-sm-12 has-error';
                    this.parentNode.getElementsByClassName('glyphicon')[0].className = 'glyphicon form-control-feedback glyphicon-remove';
                } else {
                    this.parentNode.className = 'input-group col-sm-12 has-success';
                    this.parentNode.getElementsByClassName('glyphicon')[0].className = 'glyphicon form-control-feedback glyphicon-ok';
                }
            })
        })*/
    });

    function search(num) {
        //判断上一页有没有
        if (num > 0) {
            pageBean.pageNo = num;
        } else {
            pageBean.pageNo = 1;
            num = 1;
        }
        //判断下一页有没有
        if (num <= pageBean.totalPage) {
            pageBean.pageNo = num;
        } else {
            if (pageBean.totalPage != 0) {
                pageBean.pageNo = pageBean.totalPage;
            } else {
                pageBean.pageNo = 1;
            }
        }
        //新闻标题判断
        if ($("#newstitle").val() != "") {
            if($('#newstitle').val().indexOf("'") >=0) {
                pageBean.factor.push(" 1=0 ");
            }else{
                pageBean.factor.push(" title like '%" + $("#newstitle").val() + "%'");
            }
        }
        //开始时间判断
        if ($("#ksshijian").val() != "") {
            if($('#ksshijian').val().indexOf("'") != -1) {
                pageBean.factor.push(" 1=0 ");
            }else{
                pageBean.factor.push(" createdTime >= '" + $("#ksshijian").val() + "'");
            }
        }
        //结束时间判断
        if ($("#jsshijian").val() != "") {
            if($('#jsshijian').val().indexOf("'") != -1) {
                pageBean.factor.push(" 1=0 ");
            }else{
                pageBean.factor.push(" endTime <= '" + $("#jsshijian").val() + "'");
            }
        }
        //发布人判断
        if ($("#newsname").val() != null && $("#newsname").val() != "") {
            $.ajax({
                url: "${pageContext.request.contextPath}/newsController/findStaffIdByName",
                data: {name: $("#newsname").val()},
                success: function (data) {
                    let staffId = "";
                    if (data != "" && data != null) {
                        for (let i = 0; i < data.length; i++) {
                            if (i == data.length - 1) {
                                staffId += "" + data[i]["staffId"] + "";
                            } else {
                                staffId += "" + data[i]["staffId"] + ",";
                            }
                        }
                        pageBean.factor.push(" staffId in (" + staffId + ") ");
                    } else {
                        pageBean.factor.push(" 1=0 ");
                    }
                    show();
                },
                dataType: "json"
            });
        } else {
            show();
        }
    }

    function show() {
        pageBean.data = "";
        $.post(
            "${pageContext.request.contextPath}/newsController/show",
            pageBean,
            function (date) {
                console.log(date);
                pageBean = date["page"];
                pageBean.factor = [];
                $('#rowCount').html(pageBean.rowCount);
                $('#rowstart').html(pageBean.rowCount == 0 ? 0 : pageBean.startIndex + 1);
                if(date['staff']!=null){
                    $('#xzstaff').val(date['staff']['staffName']);
                }
                let con = pageBean.rowCount == 0 ? 0 :
                    pageBean.pageNo == pageBean.totalPage ?
                        pageBean.rowCount % pageBean.pageSize == 0 ?
                            pageBean.pageSize : pageBean.rowCount % pageBean.pageSize
                        : pageBean.pageSize;
                $('#rowend').html(pageBean.startIndex + con);
                let btns = "";
                for (let i = 0; i < pageBean.totalPage; i++) {
                    if (pageBean.pageNo == i + 1) {
                        btns += "<a href='javascript:search(" + (i + 1) + ");' class='btn btn-primary'>" + (i + 1) + "</a>";
                    } else {
                        btns += "<a href='javascript:search(" + (i + 1) + ");' class='btn btn-default'>" + (i + 1) + "</a>";
                    }
                }
                $('#btns').html(btns);
                //表格显示
                let tr = "";
                if (pageBean.data != null) {
                    for (let i = 0; i < pageBean.data.length; i++) {
                        let staffname = "";
                        let status = "";
                        switch (pageBean.data[i]["status"]) {
                            case 0:
                                status = "<input type=\"button\" class=\"btn btn-danger btn-xs\" value=\"禁用\" />";
                                break;
                            case 1:
                                status = "<input type=\"button\" class=\"btn btn-info btn-xs\" value=\"启用\" />";
                                break;
                        }
                        for (let j = 0; j < date["staffs"].length; j++) {
                            if (pageBean.data[i]["staffId"] == date["staffs"][j]["staffId"]) {
                                staffname = date["staffs"][j]["staffName"];
                            }
                        }
                        tr += "<tr>" +
                            "<td>" + pageBean.data[i]['id'] + "</td>" +
                            "<td class='col-sm-5'>" + pageBean.data[i]['title'] + "</td>" +
                            "<td>" + status + "</td>" +
                            "<td>" + staffname + "</td>" +
                            "<td>" + pageBean.data[i]['createdTime'] + "</td>" +
                            "<td>" + pageBean.data[i]['endTime'] + "</td>" +
                            "<td><input type='button' class='btn btn-sm btn-info' onclick='updateTr()'  value='修改'/>" +
                            "<input type='button' class='btn btn-sm btn-danger' onclick='deteleTr()'  value='删除'/></td>" +
                            "</tr>"
                    }
                }
                $('#tb').html(tr);
            },
            "json"
        );
    }

    //删除修改按钮事件
    function deteleTr() {
        let id = event.target.parentNode.parentNode.getElementsByTagName("td")[0].innerHTML;
        let b = confirm("确定删除这个修改");
        if (b) {
            $.get(
                "${pageContext.request.contextPath}/newsController/delete",
                {id: id},
                function (pd) {
                    show();
                    if (pd > 0) {
                        toastr.success("删除成功");
                    } else {
                        toastr.error("删除失败");
                    }
                    search(1);
                }
            );
        }
    }

    //修改按钮事件
    function updateTr() {
        let id = event.target.parentNode.parentNode.firstElementChild.innerHTML;
        $.ajax({
            url: "${pageContext.request.contextPath}/newsController/selectId",
            data: {id: id},
            success: function (data) {
                $('#xiugai').modal('show');
                let inps = $('#xiugai .form-control');
                $('#xiugai [name="id"]').val(data["news"]["id"]);
                inps[0].value = data["news"]["title"];
                inps[1].value = data["staffName"];
                inps[2].value = data["news"]["status"];
                inps[3].value = data["news"]["createdTime"];
                inps[4].value = data["news"]["endTime"];
                inps[5].value = data["news"]["content"];
            },
            dataType: "json"
        });
    }

    //修改修改模态框按钮确认事件
    function xgqueren() {
        if ($('#xgform [name="title"]').val() != "" && $('#xgform [name="content"]').val() != "") {
            if ($('#xgform [name="endTime"]').val() != "") {
                if (validTime($('#xgform [name="createdTime"]').val(), $('#xgform [name="endTime"]').val())) {
                    $.post(
                        "${pageContext.request.contextPath}/newsController/update",
                        $('#xgform').parseForm(),
                        function (data) {
                            show();
                            if (data == 1) {
                                toastr.success("修改成功");
                            } else {
                                toastr.error("修改失败");
                            }
                        },
                        "json"
                    );
                    $('#xiugai').modal('hide');
                } else {
                    toastr.info("新闻开始时间时间不能超过结束时间");
                }
            } else {
                toastr.info("新闻结束时间时间不能为空");
            }
        } else {
            toastr.info("请输入新闻标题和新闻内容");
        }
    }

    //判断结束时间大于开始时间
    function validTime(startTime, endTime) {
        let arr1 = startTime.split("-");
        let arr2 = endTime.split("-");
        let date1 = new Date(parseInt(arr1[0]), parseInt(arr1[1]) - 1, parseInt(arr1[2]), 0, 0, 0);
        let date2 = new Date(parseInt(arr2[0]), parseInt(arr2[1]) - 1, parseInt(arr2[2]), 0, 0, 0);
        if (date1.getTime() > date2.getTime()) {
            return false;
        } else {
            return true;
        }
        return false;
    }

    // $('#btns a').each(function () {
    //     this.className="btn btn-default";
    //     $(this).click(function () {
    //         this.className="btn btn-info";
    //     })
    // });
    //开始时间
    laydate.render({
        elem: '#ksshijian' //指定元素
    });
    //结束时间
    laydate.render({
        elem: '#jsshijian' //指定元素
    });
    //新增结束时间
    laydate.render({
        elem: '#jieshusj1'
    });
    //修改创建时间
    laydate.render({
        elem: '#cjshijian1' //指定元素
    });
    //新增结束时间
    laydate.render({
        elem: '#jieshusj'
    });
    //新增创建时间
    laydate.render({
        elem: '#cjshijian' //指定元素
    });

</script>
</html>
